/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2015 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: Kevin xu                                                     |
  +----------------------------------------------------------------------+
*/

/**
 * configuration as following:
 * extension=scc.so
 *
 * [SCC]
 * scc.zk_addrs=127.0.0.1:2181
 * scc.service_declare_config=/home/kevin/development/ext/scc/declares.json
 *
 *
 */

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_scc.h"
#include "log.h"
#include "proxy_invoke.h"


#include <stdio.h>
#include <string.h>


/* If you declare any globals in php_scc.h uncomment this:*/
ZEND_DECLARE_MODULE_GLOBALS(scc)


/* True global resources - no need for thread safety here */
static int le_scc;

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
*/
/* }}} */
PHP_INI_BEGIN()
// STD_PHP_INI_ENTRY("scc.zk_addrs",               "127.0.0.1:2181", PHP_INI_ALL, OnUpdateString, g_zk_addrs, zend_scc_globals, scc_globals)
// STD_PHP_INI_ENTRY("scc.service_declare_config", "./declares.json", PHP_INI_ALL, OnUpdateString, g_service_declare_config, zend_scc_globals, scc_globals)
STD_PHP_INI_ENTRY("scc.proxy_sock",             "/tmp/soa_proxy.sock", PHP_INI_ALL, OnUpdateString, g_proxy_sock, zend_scc_globals, scc_globals)
STD_PHP_INI_ENTRY("scc.log_path",               "/tmp/soa/consumer.log", PHP_INI_ALL, OnUpdateString, g_log_path, zend_scc_globals, scc_globals)
STD_PHP_INI_ENTRY("scc.log_level",              "info", PHP_INI_ALL, OnUpdateString, g_log_level, zend_scc_globals, scc_globals)
STD_PHP_INI_ENTRY("scc.log_lock_type",          "thread", PHP_INI_ALL, OnUpdateString, g_log_lock_type, zend_scc_globals, scc_globals)
STD_PHP_INI_ENTRY("scc.log_rotate_size",        "104857600", PHP_INI_ALL, OnUpdateLongGEZero, g_log_rotate_size, zend_scc_globals, scc_globals)
PHP_INI_END()


/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_scc_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_scc_compiled)
{
  char *arg = NULL;
  int arg_len, len;
  char *strg;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
    return;
  }

  len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "scc", arg);
  RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   follow this convention for the convenience of others editing your code.
*/

/* {{{ proto string invoke_service(string group_name, string service_name, string version, string req_body)
    */
PHP_FUNCTION(invoke_service)
{
  char *group_name = NULL;
  char *service_name = NULL;
  char *version = NULL;
  char *req_body = NULL;
  int argc = ZEND_NUM_ARGS();
  int group_name_len;
  int service_name_len;
  int version_len;
  int req_body_len;

  if (zend_parse_parameters(argc TSRMLS_CC, "ssss", &group_name, &group_name_len, &service_name, &service_name_len, &version, &version_len, &req_body, &req_body_len) == FAILURE)
    return;

  char * res_buffer = NULL;
  size_t res_buf_len;
  if (!invoke_proxy(SCC_G(g_proxy_sock), group_name, service_name, version, req_body, req_body_len, &res_buffer, &res_buf_len)) {
    ZVAL_STRINGL(return_value, res_buffer, res_buf_len, 1);
    if (res_buffer) {
      free(res_buffer);
    }
  } else {
    RETVAL_NULL();
  }

}
/* }}} */


/* {{{ proto void log_output(string logger, int level, string message)
   ...) */
PHP_FUNCTION(front_log_output)
{
  char *logger = NULL;
  char *message = NULL;
  int argc = ZEND_NUM_ARGS();
  int logger_len;
  int message_len;
  long level;

  if (zend_parse_parameters(argc TSRMLS_CC, "sls", &logger, &logger_len, &level, &message, &message_len) == FAILURE)
    return;

  log_output(logger, level, "%s", message);
}
/* }}} */


/* {{{ php_scc_init_globals
 */
/* Uncomment this function if you have INI entries
*/
/* }}} */
static void php_scc_init_globals(zend_scc_globals * scc_globals)
{
  // scc_globals->g_zk_addrs = NULL;
  // scc_globals->g_service_declare_config = NULL;
  scc_globals->g_proxy_sock = NULL;
  scc_globals->g_log_path = NULL;
  scc_globals->g_log_level = NULL;
  scc_globals->g_log_lock_type = NULL;
  scc_globals->g_log_rotate_size = 1024 * 1024 * 100;
}


/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(scc)
{
  /* If you have INI entries, uncomment these lines
  */
  ZEND_INIT_MODULE_GLOBALS(scc, php_scc_init_globals, NULL);  //!!!import , or Segmentation fault
  REGISTER_INI_ENTRIES();
  //php_printf("PHP_MINIT_FUNCTION : %s,%s\n", SCC_G(g_zk_addrs), SCC_G(g_service_declare_config));

  if (open_log(SCC_G(g_log_path), SCC_G(g_log_level), SCC_G(g_log_lock_type), SCC_G(g_log_rotate_size))) {
    return FAILURE;
  }

  return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(scc)
{
  /* uncomment this line if you have INI entries
  */
  UNREGISTER_INI_ENTRIES();
  // php_printf("PHP_MSHUTDOWN_FUNCTION\n");

  close_log();

  return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(scc)
{
  // php_printf("PHP_RINIT_FUNCTION\n");
  return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(scc)
{
  // php_printf("PHP_RSHUTDOWN_FUNCTION\n");
  return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(scc)
{
  php_info_print_table_start();
  php_info_print_table_header(2, "scc support", "enabled");
  php_info_print_table_end();

  /* Remove comments if you have entries in php.ini
  */
  DISPLAY_INI_ENTRIES();

}
/* }}} */

/* {{{ scc_functions[]
 *
 * Every user visible function must have an entry in scc_functions[].
 */
const zend_function_entry scc_functions[] = {
  PHP_FE(confirm_scc_compiled,    NULL)       /* For testing, remove later. */
  PHP_FE(invoke_service,  NULL)
  PHP_FE(front_log_output,  NULL)
  PHP_FE_END  /* Must be the last line in scc_functions[] */
};
/* }}} */

/* {{{ scc_module_entry
 */
zend_module_entry scc_module_entry = {
  STANDARD_MODULE_HEADER,
  "scc",
  scc_functions,
  PHP_MINIT(scc),
  PHP_MSHUTDOWN(scc),
  PHP_RINIT(scc),     /* Replace with NULL if there's nothing to do at request start */
  PHP_RSHUTDOWN(scc), /* Replace with NULL if there's nothing to do at request end */
  PHP_MINFO(scc),
  PHP_SCC_VERSION,
  STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_SCC
ZEND_GET_MODULE(scc)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
